package com.example.administrator.zhongyichou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.zhongyichou.R;
import com.example.administrator.zhongyichou.help.DateUtils;
import com.example.administrator.zhongyichou.help.ImageLoader;
import com.example.administrator.zhongyichou.net.protocol.MutualPublicProtocol;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lichenyu on 2017/11/24.
 * 描述：
 */

public class MutualPublicAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<MutualPublicProtocol.DataBean> list;

    public MutualPublicAdapter(Context context, List<MutualPublicProtocol.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getGroupCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getMutual_help() == null ? 0 : list.get(groupPosition).getMutual_help().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.mutual_public_list_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.txtTitle.setText(list.get(groupPosition).getType_title());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder1 holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.mutual_public_list_item1, null);
            holder = new ViewHolder1(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder1) convertView.getTag();
        }
        holder.txtName.setText("被救助人：" + list.get(groupPosition).getMutual_help().get(childPosition).getHelp_name());
        holder.txtTime.setText("公式日期: " + DateUtils.stampToDateDay(list.get(groupPosition).getMutual_help().get(childPosition).getActive_time()));
        ImageLoader.centerCropUrl(context, list.get(groupPosition).getMutual_help().get(childPosition).getHelp_img(), holder.img);
        if (list.get(groupPosition).getMutual_help().get(childPosition).getIs_audit().equals("0")) {
            holder.imgStatus.setVisibility(View.GONE);
        } else {
            holder.imgStatus.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    static class ViewHolder {
        @BindView(R.id.txt_title)
        TextView txtTitle;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder1 {
        @BindView(R.id.img)
        ImageView img;
        @BindView(R.id.img_status)
        ImageView imgStatus;
        @BindView(R.id.txt_name)
        TextView txtName;
        @BindView(R.id.txt_time)
        TextView txtTime;

        ViewHolder1(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
